import axios from "axios"
import type {
	AxiosInstance,
	AxiosRequestConfig,
	InternalAxiosRequestConfig,
	AxiosResponse,
} from "axios"
import { ElMessage } from "element-plus"

const config = {
	baseURL: "/api",
	timeout: 10000,
}

export interface Result<T = never> {
	code: number
	message: string
	data: T
}

class Http {
	private instance: AxiosInstance

	constructor(config: AxiosRequestConfig) {
		this.instance = axios.create(config)
		this.interceptors()
	}

	/* eslint-disable @typescript-eslint/no-explicit-any */

	private interceptors() {
		// eslint-disable-next-line @typescript-eslint/no-unused-expressions
		this.instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
			const token: string = ""
			if (token) {
				config.headers!["Authorization"] = token
			}
			return config
		}),
			(error: any) => {
				error.data = {}
				error.data.message = "服务器异常,请联系管理员"
				return error
			}

		// eslint-disable-next-line @typescript-eslint/no-unused-expressions
		this.instance.interceptors.response.use((res: AxiosResponse) => {
			if (res.data.code === 200) {
				return res.data
			} else {
				ElMessage.error(res.data.message || "服务器出错")
				return Promise.reject(res.data.message || "服务器出错")
			}
		}),
			(error: any) => {
				console.log("出错了")
				error.data = {}
				if (error && error.response) {
					switch (error.response.status) {
						case 400:
							error.data.message = "请求错误"
							ElMessage.error(error.data.message)
							break
						case 401:
							error.data.message = "未授权,请重新登录"
							ElMessage.error(error.data.message)
							break
						case 403:
							error.data.message = "拒绝访问"
							ElMessage.error(error.data.message)
							break
						case 404:
							error.data.message = "请求错误,未找到接口"
							ElMessage.error(error.data.message)
							break
						case 405:
							error.data.message = "请求方法不被允许"
							ElMessage.error(error.data.message)
							break
						case 408:
							error.data.message = "请求超时"
							ElMessage.error(error.data.message)
							break
						case 500:
							error.data.message = "服务器端出错"
							ElMessage.error(error.data.message)
							break
						case 501:
							error.data.message = "网络未实现"
							ElMessage.error(error.data.message)
							break
						case 502:
							error.data.message = "完了错误"
							ElMessage.error(error.data.message)
							break
						case 503:
							error.data.message = "服务不可用"
							ElMessage.error(error.data.message)
							break
						case 504:
							error.data.message = "网络超时"
							ElMessage.error(error.data.message)
							break
						case 505:
							error.data.message = "http版本不支持该请求"
							ElMessage.error(error.data.message)
							break
						default:
							error.data.message = `连接错误:${error.response.status}`
							ElMessage.error(error.data.message)
					}
				} else {
					error.data.message = "连接到服务器失败"
					ElMessage.error(error.data.message)
				}
				return Promise.reject(error)
			}
	}

	/* eslint-enable @typescript-eslint/no-explicit-any */

	get<T = Result>(path: string, params?: object): Promise<T> {
		return this.instance.get(path, { params })
	}

	post<T = Result>(path: string, data?: object): Promise<T> {
		return this.instance.post(path, data)
	}

	put<T = Result>(path: string, data?: object): Promise<T> {
		return this.instance.put(path, data)
	}

	delete<T = Result>(path: string): Promise<T> {
		return this.instance.delete(path)
	}

	upload<T = Result>(path: string, data: object): Promise<T> {
		return this.instance.post(path, data, {
			headers: {
				"Content-Type": "multipart/form-data",
			},
		})
	}
}

export default new Http(config)
